import request, { type ListQuery } from "@/utils/request";

const GOODS_BASE_URL = "/api";
const GoodsAPI = {
  /**
   * 供应商上架商品
   *
   * @param queryParams 请求参数
   */
  postGoods(queryParams: GoodsQueryForm) {
    return request<any>({
      url: `${GOODS_BASE_URL}/products/`,
      method: "post",
      data: queryParams,
    });
  },
  /**
   * 供应商编辑商品
   *
   * @param queryParams 请求参数
   * @param id
   */
  putGoods(queryParams: GoodsQueryForm, id: number) {
    return request<any>({
      url: `${GOODS_BASE_URL}/products/${id}/`,
      method: "put",
      data: queryParams,
    });
  },
  /**
   * 获取商品详情
   *
   * @param id 商品id
   */
  getGoodsDetail(id: number) {
    return request<any>({
      url: `${GOODS_BASE_URL}/products/${id}/`,
      method: "get",
    });
  },
  /**
   * 获取商品分页列表
   *
   * @param queryParams 查询参数
   */
  getPage(queryParams: {}) {
    return request<any, PageResult<GoodsPageVO[]>>({
      url: `${GOODS_BASE_URL}/products/`,
      method: "get",
      params: queryParams,
    });
  },
  getCategories: (params: any) => {
    return request.get(`${GOODS_BASE_URL}/product/categories/`, {
      params,
    });
  },
  /**
   * @description 删除商品分类
   * @param id 分类id
   * */
  deleteCategory(id: number) {
    return request<any, undefined>({
      url: `${GOODS_BASE_URL}/product/categories/${id}/`,
      method: "delete",
    });
  },
  /**
   * 获取商品品牌
   *
   * @param queryParams 查询参数
   */
  getBrand(queryParams: {}) {
    return request<any, PageResult<[]>>({
      url: `${GOODS_BASE_URL}/brands/`,
      method: "get",
      params: queryParams,
    });
  },
  /**
   * 获取商品单位列表
   * @return {Promise<UomItem[]>} 单位列表
   */
  getUom(): Promise<UomItem[]> {
    return request<any, UomItem[]>({
      url: `${GOODS_BASE_URL}/product/uom/`,
      method: "get",
    });
  },
  createUom(data: UomFormData) {
    return request<any, UomItem>({
      url: `${GOODS_BASE_URL}/product/uom/`,
      method: "post",
      data,
    });
  },
  deleteUom(id: number) {
    return request<any, undefined>({
      url: `${GOODS_BASE_URL}/product/uom/${id}/`,
      method: "delete",
    });
  },
  updateUom(id?: number, data?: UomFormData) {
    return request<any, UomItem>({
      url: `${GOODS_BASE_URL}/product/uom/${id}/`,
      method: "put",
      data,
    });
  },
  activityList(params: any) {
    return request<any, any>({
      url: `${GOODS_BASE_URL}/products/promotional/`,
      method: "get",
      params,
    });
  },
  addActivity(data: any) {
    return request<any, any>({
      url: `${GOODS_BASE_URL}/products/promotional/publish/`,
      method: "post",
      data,
    });
  },
  activityDetail(id: any) {
    return request<any, any>({
      url: `${GOODS_BASE_URL}/products/promotional/${id}/`,
      method: "get",
    });
  },
};
export default GoodsAPI;

/**
 * 商品分页查询对象
 */
export interface goodsPageQuery {
  /** 搜索关键字 */
  search?: string;
  /** 搜索分类ID */
  category?: string;
  /** 搜索供应商ID */
  supplier?: string;
  /** 排序 */
  ordering?: [string];
  /** 页码 */
  page?: number;
  /** 页数 */
  size?: number;
}

/**
 * 商品分页VO
 */
export interface GoodsPageVO {}

/**
 * @description 单位列表项
 * @param id 单位ID
 * @param group 单位组
 * @param name 单位名称
 * */
export interface UomItem {
  id: number;
  group: {
    id: number;
    name: string;
  };
  name: string;
}

/**
 * @description 新增单位
 * @param name 单位名称
 * @param group 单位组ID
 * */
export interface UomFormData {
  id?: number;
  name: string;
  group?: number;
}

/**
 * 新增商品参数
 */

export type GoodsQueryForm = {
  supplier__in: number;
} & ListQuery;
